package cn.kgc.springcloud.service.impl;

import cn.kgc.springcloud.client.CityClient;
import cn.kgc.springcloud.client.ClazzClient;
import cn.kgc.springcloud.client.GroupClient;
import cn.kgc.springcloud.client.TagClient;
import cn.kgc.springcloud.dto.StudentDTO;
import cn.kgc.springcloud.entity.*;
import cn.kgc.springcloud.mapper.StudentTagMapper;
import cn.kgc.springcloud.util.DateUtil;
import cn.kgc.springcloud.vo.StudentVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springcloud.service.StudentService;
import cn.kgc.springcloud.mapper.StudentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClazzClient clazzClient;
    @Autowired
    private GroupClient groupClient;
    @Autowired
    private CityClient cityClient;

    @Autowired
    private TagClient tagClient;
    @Autowired
    private StudentTagMapper studentTagMapper;


    @Override
    public List<StudentVO> getList() {

        List<Student> students = studentMapper.selectList(null);
        // 遍历学生信息列表  实现数据转换 student -> studentVO
        List<StudentVO> collect = students.stream().map(student -> {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);

            // 根据班级的id 查询班级详细信息
            Clazz clazz = clazzClient.getById(student.getClazzid());
            studentVO.setClazz(clazz);
            // 根据小组id查询小组详细信息
            Group group = groupClient.getById(student.getGroupid());
            studentVO.setGroup(group);
            // 根据城市id 查询城市详细信息
            City city = cityClient.getById(student.getCityid());
            studentVO.setCity(city);

            /**
             *查询标签列表   student  - > tag   多对多关系
             * 1.获取学生id
             * 2.根据学生的id查询 学生标签关系表  标签的id
             * 3.根据标签id 查询标签的详细信息
             */
            Integer studentId = student.getId();
            LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
            lambda.eq(StudentTag::getSid, studentId);
            List<StudentTag> studentTags = studentTagMapper.selectList(lambda);
            // 遍历studentTags  获取所有标签id

            List<Integer> tids = studentTags.stream().map(studentTag -> {
                return studentTag.getTid();
            }).collect(Collectors.toList());

            //3.根据标签id 查询标签的详细信息
            List<Tag> tags = tids.stream().map(tid -> {
                Tag tag = tagClient.getById(tid);
                return tag;
            }).collect(Collectors.toList());

            studentVO.setTags(tags);

            return studentVO;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public StudentVO addStudent(StudentDTO studentDTO) {

        Student student = new Student();
        BeanUtils.copyProperties(studentDTO, student);

        //获取学生的生日  计算 年龄 星座 属相
        Date bir = student.getBir();

        int age = DateUtil.getAge(bir);
        String year = DateUtil.getYear(bir);
        String constellation = DateUtil.getConstellation(bir);

        student.setAge(age);
        student.setAttr(year);
        student.setStarts(constellation);

        studentMapper.insert(student); // id

        // 获取所有的标签id
        List<Integer> tagIds = studentDTO.getTagids();
        Integer studentId = student.getId();

        tagIds.forEach(tagId->{
            StudentTag studentTag = new StudentTag();
            studentTag.setTid(tagId);
            studentTag.setSid(studentId);
            studentTagMapper.insert(studentTag);
        });

        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student, studentVO);

        //查询城市信息
        City city = cityClient.getById(student.getCityid());
        studentVO.setCity(city);
        // 查询小组信息
        Group group = groupClient.getById(student.getGroupid());
        studentVO.setGroup(group);
        // 查询班级信息
        Clazz clazz = clazzClient.getById(student.getClazzid());
        studentVO.setClazz(clazz);

        // 查询标签信息
        LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
        lambda.eq(StudentTag::getSid, studentId);
        List<StudentTag> studentTags = studentTagMapper.selectList(lambda);
        // 遍历studentTags  获取所有标签id
        List<Integer> tids = studentTags.stream().map(studentTag -> {
            return studentTag.getTid();
        }).collect(Collectors.toList());

        //3.根据标签id 查询标签的详细信息
        List<Tag> tags = tids.stream().map(tid -> {
            Tag tag = tagClient.getById(tid);
            return tag;
        }).collect(Collectors.toList());

        studentVO.setTags(tags);
        return studentVO;
    }


}




